import React from "react"
import * as util from "../../util"
import { getSearchList } from "../../../fetch/search";
import ListComponentsBySearch from "../ListComponentsBySearch";

export default class SearchList extends React.Component{
	constructor(){
		super();
		this.state = {
			hasMore : false,
			data : [],
			keywords: '',
			page : 0,
			city : ''
		}
	}
	componentDidMount(){
		this.resultHttp();
	}
	//该生命周期非常关键 组件传值改变之时会被处发该函数执行
	componentDidUpdate(prevProps,prevState){
		if(this.props.keywords === prevProps.keywords){
			return;
		}
		this.resultHttp();
	}

	resultHttp(){
		let city = util.storage.get('city');
		let keywords = this.props.keywords;
		
		this.setState({
			city: city
		});

		var result = getSearchList(city,keywords,0);
		result.then(r=>{
			this.setState({
				data : r.data,
				hasMore : r.hasMore
			});
		});
	}
	render(){
		// let { keywords , city } = this.state;
		let keywords = this.props.keywords;
		return(
			<div>
				{
					this.state.data.length>0
					?<ListComponentsBySearch data={ this.state.data } />
					:<div>正在请求数据</div>
				}
			</div>
		)
	}
}
